home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9174 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  41 lines

  1. Path: crl.crl.com!not-for-mail
  2. From: bobfry@crl.com (Robert Fry)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Q: tokenizing
  5. Date: 8 Mar 1996 08:35:32 -0800
  6. Organization: CRL Dialup Internet Access
  7. Message-ID: <4hpnkk$lkh@crl.crl.com>
  8. References: <maguirer.826209754@hercules>
  9. NNTP-Posting-Host: crl.com
  10.  
  11. maguirer@HERCULES.CS.UREGINA.CA (Rob Maguire) writes:
  12.  
  13. >Have read the FAQ and found the question where it talks about separating a
  14. >string into tokens split by white-space.  The answer is to use 'strtok'.
  15.  
  16. >OK...  How?
  17.  
  18. >Have tried, with little luck so far.
  19.  
  20. What do you have so far? An example of how far you've gotten might help.
  21.  
  22. Here's an off-the-cuff example (which is probably buggy, as I can't check 
  23. it easily):
  24.  
  25. char string[] = "A sample string to be made into tokens";
  26. char *tokens[10];
  27. int i;
  28.  
  29. tokens[ 0] = strtok( string, " ");
  30. for ( i = 1; tokens[ i - 1]; i++)
  31.     tokens[ i] = strtok( string, NULL);
  32.  
  33. /* When you leave the loop, tokens[ 0] points to "A", tokens[ 1] points 
  34. to "sample", tokens[ 2] points to "string", and so on. */
  35.  
  36. Note that this example's completely lacking in error checking, and if I 
  37. was willing to take the time, I'd probably put in a big comment about the 
  38. loop condition.
  39.  
  40.   Bob
  41.